package com.aat.dizena.base.web.controller;

import java.util.*;

import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import com.aat.dxfy.utils.Atools;
import com.aat.dizena.base.web.controller.BaseController;

import com.aat.dizena.base.bean.User;
import com.aat.dizena.base.bean.User0info;
import com.aat.dizena.base.service.User0infoService;
import com.aat.dizena.base.service.UserService;

/**
 * @time 2016-05-25
 * @author 新歌<xingeking@163.com>
 * @email xingeking@163.com
 * @version v1.0
 * @function 用户的管理
 * @info java spring aop
 * @update none
 */
@Controller
@Scope("prototype")
@RequestMapping("master/user")
public class MasterUserController extends BaseController {

	@Autowired
	private UserService userService;

	@Autowired
	private User0infoService user0infoService;

	private String list = "/master/user/list";
	private String edit = "/master/user/edit";
	private String reList = "redirect:" + list;
	private String addAction = "master/user/add";
	private String uptAction = "master/user/upt";
	private String delAction = "master/user/del";
	private String StringKey = " ";
	private String addTitle = "增加" + StringKey;
	private String uptTitle = "修改" + StringKey;
	private String listHead = StringKey + "管理";

	// list get
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model m) {
		m.addAttribute("addAction", addAction);
		m.addAttribute("uptAction", uptAction);
		m.addAttribute("delAction", delAction);
		m.addAttribute("listHead", listHead);
		m.addAttribute("list", list);
		return list;
	}

	// list post
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> list(@RequestParam(value = "s", required = false, defaultValue = "1") int start,
			@RequestParam(value = "p", required = false, defaultValue = "10") int perPage,
			@RequestParam(value = "f", required = false, defaultValue = "ts") String sortFiled,
			@RequestParam(value = "dir", required = false, defaultValue = "DESC") String direction,

	@RequestParam(value = "u", required = false) String skey) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sortFiled", sortFiled);
		params.put("direction", direction);
		params.put("start", (start - 1) * perPage);
		params.put("perpage", perPage);
		// 其他参数
		if (isNotEmpty(skey)) {
			params.put("skey", skey);
		}

		// 返回参数
		List<User> lists = userService.queryAll(params);
		Long count = userService.queryCount(params);
		Long tmp = count / perPage;
		Long pages = count % perPage == 0 ? tmp : tmp + 1;
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("datas", lists);// 数据
		json.put("pages", pages);// 总页
		json.put("start", start);// 页码
		json.put("count", count);// 总数
		return json;
	}

	// add get
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute("action", addAction);
		model.addAttribute("title", addTitle);
		User m = new User();
		m.setId(Atools.getOneKeyS());
		model.addAttribute("m", m);
		return edit;
	}

	// add post
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(User m, Model model) {
		model.addAttribute("action", addAction);
		model.addAttribute("title", addTitle);
		model.addAttribute("m", m);
		// 验证
		if (isEmpty(m.getAccount())) {
			model.addAttribute("error", "登录账户不为空");
			return edit;
		} else {
			User tmp = userService.queryUserLogin(m.getAccount());
			if (tmp != null) {
				model.addAttribute("error", "账户已经存在了");
				return edit;
			}
		}

		if (isEmpty(m.getStr01())) {
			model.addAttribute("error", "联系邮箱不为空");
			return edit;
		} else {
			User tmp = userService.queryUserLogin(m.getStr01());
			if (tmp != null) {
				model.addAttribute("error", "邮箱已使用过请更换或找回密码了");
				return edit;
			}
		}

		if (isEmpty(m.getPasswd())) {
			model.addAttribute("error", "登录密码不为空");
			return edit;
		}
		if (!m.getPasswd().equals(m.getSalt())) {
			model.addAttribute("error", "密码输入不一致！");
			return edit;
		}
		if (isEmpty(m.getNickname())) {
			model.addAttribute("error", "用户昵称不为空");
			return edit;
		}
		m.setFlag("addByMaster");
		// end
		//
		String salt = Atools.jiami(m.getPasswd());
		String pwd = Atools.makePwd(m.getPasswd() + salt);
		m.setPasswd(pwd);
		m.setSalt(salt);
		m.setTs(new Date());
		//
		userService.addBean(m);
		//
		User0info u0 = new User0info();
		u0.setId(Atools.getOneKeyS());
		u0.setUserid(m.getId());
		u0.setEmail(m.getStr01());
		u0.setTs(new Date());
		user0infoService.addBean(u0);
		//
		return reList;
	}

	// upt get
	@RequestMapping(value = "upt/{id}", method = RequestMethod.GET)
	public String upt(@PathVariable("id") String id, Model model) {
		model.addAttribute("action", uptAction);
		model.addAttribute("title", uptTitle);
		User m = userService.queryBean(id);
		model.addAttribute("m", m);
		return edit;
	}

	// upt post
	@RequestMapping(value = "upt", method = RequestMethod.POST)
    public String upt(User m,Model model) {
        model.addAttribute("action", uptAction);
        model.addAttribute("title", uptTitle);
        model.addAttribute("m", m);
        // 验证
        if(isEmpty(m.getAccount())){
			model.addAttribute("error", "登录账户不为空");
			return edit;
		}else{
			User tmp = userService.queryUserLogin(m.getAccount());
			if(tmp!=null){
				model.addAttribute("error", "账户已经存在了");
				return edit;
			}
		}
        
        if(isEmpty(m.getStr01())){
			model.addAttribute("error", "联系邮箱不为空");
			return edit;
		}else{
			User tmp = userService.queryUserLogin(m.getStr01());
			if(tmp!=null){
				model.addAttribute("error", "邮箱已使用过请更换或找回密码了");
				return edit;
			}
		}
        
        if(isEmpty(m.getPasswd())){
			model.addAttribute("error", "登录密码不为空");
			return edit;
		}
		if(!m.getPasswd().equals(m.getSalt())){
			model.addAttribute("error", "密码输入不一致！");
			return edit;
		}
		if(isEmpty(m.getNickname())){
			model.addAttribute("error", "用户昵称不为空");
			return edit;
		}
        //end 
        String salt=Atools.jiami(m.getPasswd());
        String pwd=Atools.makePwd(m.getPasswd()+salt);
        m.setPasswd(pwd);
        m.setSalt(salt);
        //
        userService.updateBean(m);
        
        //
        User0info u0=  user0infoService.queryUinfo(m.getId());
        u0.setEmail(m.getStr01());
        user0infoService.updateBean(u0);
        return reList;
    }

	// del
	@RequestMapping(value = "del/{id}", method = RequestMethod.GET)
	public String del(@PathVariable("id") String id) {
		userService.deleteBean(id);
		return reList;
	}

	// locked
	@RequestMapping(value = "lock")
	public String lockUser(@RequestParam("id") String id, @RequestParam("s") String s) {
		if (chkRole("master") || chkAuth("*:admin:*")) {
			User u = new User();
			u.setId(id);
			u.setLocked(s);
			userService.updateBean(u);
		}
		return reList;
	}

}
